package com.seed.fileud.controller;

import com.seed.fileud.pojo.FileDTO;
import com.seed.fileud.pojo.FileStatus;
import com.seed.fileud.service.FileudService;
import com.seed.core.pojo.R;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.OutputStream;

@RestController
public class FileudController {

    private FileudService fileudService;

    public FileudController(FileudService fileudService) {
        this.fileudService = fileudService;
    }

    /**
     * 验证文件是否存在
     */
    @GetMapping("validate")
    public R<FileStatus> validate(FileDTO dto, Integer chunkCount) {
        FileStatus status = fileudService.validate(dto.getBucket(), dto.getPath(), dto.getHash(), dto.getFileName(), chunkCount);
        return R.data(status);
    }

    @PostMapping("uploadFile")
    public R<String> uploadFile(@Valid FileDTO dto) {
        String fileName = fileudService.uploadFile(dto.getBucket(), dto.getPath(), dto.getHash(), dto.getFile());
        return R.data(fileName);
    }

    @PostMapping("uploadChunk")
    public R<String> uploadChunk(@Valid FileDTO dto) {
        fileudService.uploadChunk(dto.getBucket(), dto.getPath(), dto.getHash(), dto.getChunkIndex(), dto.getFile());
        return R.ok("");
    }

    /**
     * 文件合并
     * @param dto
     * @return
     */
    @GetMapping("merge")
    public R<String> merge(FileDTO dto) {
        String fileName = fileudService.merge(dto.getBucket(), dto.getPath(), dto.getHash(), dto.getFileName());
        return R.data(fileName);
    }

    @DeleteMapping("remove")
    public R<String> remove(String filePath) {
        fileudService.remove(filePath);
        return R.ok("");
    }

    /**
     * 获取文件大小
     * @param filePath
     * @return
     */
    @GetMapping("/fileSize")
    public R<Long> fileSize(String filePath) {
        File file = fileudService.file(filePath);
        return R.data(file.length());
    }

    /**
     * 下载文件
     * @param filePath
     * @param response
     * @throws Exception
     */
    @GetMapping("/downloadFile")
    public void downloadFile(String filePath, HttpServletResponse response) throws Exception {
        File file = fileudService.file(filePath);
        InputStream inputStream = new FileInputStream(file);
        OutputStream outputStream = response.getOutputStream();
        response.setContentType("application/octet-stream; charset=UTF-8");
        byte[] buffer = new byte[1024];
        while (inputStream.read(buffer) != -1) {
            outputStream.write(buffer);
        }

        inputStream.close();
        outputStream.close();
    }

    /**
     * 下载文件块
     * @param filePath
     * @param offset
     * @param length
     * @param response
     * @throws Exception
     */
    @GetMapping("/downloadChunk")
    public void downloadChunk(String filePath, Long offset, Long length, HttpServletResponse response) throws Exception {
        byte[] bytes = fileudService.readFile(filePath, offset, length);
        OutputStream outputStream = response.getOutputStream();
        response.setContentType("application/octet-stream; charset=UTF-8");
        outputStream.write(bytes);
        outputStream.close();
    }

//    @GetMapping("/getDirectories")
//    public R<List<Node>> getDirectories() {
//        return R.data(FileKit.getDirectories("E:/uploadFiles/", "E:/uploadFiles/"));
//    }
//
//    @GetMapping("/getFiles")
//    public R<List<FileVO>> getFiles(String path) {
//        return R.data(FileKit.getFiles(path, "E:/uploadFiles/"));
//    }
}
